import logging

from PyQt5.QtCore import Qt, QObject, QTimer, QEvent, QPoint
from PyQt5.QtWidgets import QFrame

from .business_card_back_ui import *
from .business_card_front_ui import *


class BusinessCard(QObject):
    def __init__(self):
        super(BusinessCard, self).__init__()
        self.front = BusinessCardFront()
        self.front.show()

        self.back = BusinessCardBack()
        self.signal_init()

    def signal_init(self):
        self.front.ui.exit_pb.clicked.connect(self.on_front_exit_button_clicked)
        self.back.ui.exit_pb.clicked.connect(self.on_back_exit_button_clicked)

        self.front.ui.contact_pb.clicked.connect(self.on_contact_button_clicked)
        self.back.ui.back_pb.clicked.connect(self.on_back_button_clicked)

    def on_front_exit_button_clicked(self):
        self.front.close()
        self.deleteLater()

    def on_back_exit_button_clicked(self):
        logging.info("on_back_exit_button_clicked")
        self.back.close()
        self.deleteLater()

    def on_contact_button_clicked(self):
        self.front.hide()
        self.back.show()

    def on_back_button_clicked(self):
        logging.info("on_back_button_clicked")
        try:
            self.back.hide()
            self.front.show()
        except Exception as e:
            logging.info(e)


def custom_widget(w: QFrame):
    w.setWindowFlags(Qt.FramelessWindowHint)
    w.setAutoFillBackground(True)
    w.setStyleSheet(widget_css)


class BusinessCardFront(QFrame):
    def __init__(self):
        super(BusinessCardFront, self).__init__()
        self.ui = Ui_business_card_front()
        self.ui.setupUi(self)
        custom_widget(self)
        self.back = None
        self.blink_timer = None
        self.blink_state = True
        self.relative_pos: QPoint = QPoint(0, 0)
        self.button_blink()

    def button_blink(self):
        self.ui.exit_pb.installEventFilter(self)
        self.ui.contact_pb.installEventFilter(self)
        self.blink_timer = QTimer()
        self.blink_timer.timeout.connect(self.on_button_blank_timeout)
        self.blink_timer.start(1000)

    def on_button_blank_timeout(self):
        try:
            if self.blink_state is False:
                self.blink_state = True
                self.ui.exit_pb.show()
                self.ui.contact_pb.show()
            else:
                self.blink_state = False
                self.ui.exit_pb.hide()
                self.ui.contact_pb.hide()
        except Exception as e:
            logging.info(e)

    def eventFilter(self, a0: 'QObject', a1: 'QEvent') -> bool:
        if a1.type() == QEvent.HoverEnter:
            self.blink_timer.stop()
        elif a1.type() == QEvent.HoverLeave:
            self.blink_timer.start(1000)
        return super().eventFilter(a0, a1)

    def mousePressEvent(self, event: QtGui.QMouseEvent) -> None:
        logging.info("mousePressEvent")
        if event.button() == Qt.LeftButton:
            logging.info("left button pressed")
            self.relative_pos = self.pos() - event.globalPos()
        elif event.button() == Qt.RightButton:
            self.relative_pos = QPoint(0, 0)
        super().mousePressEvent(event)

    def mouseMoveEvent(self, event: QtGui.QMouseEvent) -> None:
        if self.relative_pos != QPoint(0, 0):
            self.move(event.globalPos() + self.relative_pos)

        super().mouseMoveEvent(event)

    def mouseReleaseEvent(self, a0: QtGui.QMouseEvent) -> None:
        logging.info("mouseReleaseEvent")
        logging.info(a0)
        if a0.button() == Qt.LeftButton:
            self.relative_pos = QPoint(0, 0)
        super().mouseReleaseEvent(a0)


class BusinessCardBack(QFrame):
    def __init__(self):
        super(BusinessCardBack, self).__init__()
        self.ui = Ui_business_card_back()
        self.ui.setupUi(self)
        custom_widget(self)

        self.back = None
        self.blink_timer = None
        self.blink_state = True
        self.relative_pos: QPoint = QPoint(0, 0)
        self.button_blink()

    def button_blink(self):
        self.ui.back_pb.installEventFilter(self)
        self.ui.exit_pb.installEventFilter(self)
        self.blink_timer = QTimer()
        self.blink_timer.timeout.connect(self.on_button_blank_timeout)
        self.blink_timer.start(1000)

    def on_button_blank_timeout(self):
        try:
            if self.blink_state is False:
                self.blink_state = True
                self.ui.exit_pb.show()
                self.ui.back_pb.show()
            else:
                self.blink_state = False
                self.ui.exit_pb.hide()
                self.ui.back_pb.hide()
        except Exception as e:
            logging.info(e)

    def eventFilter(self, a0: 'QObject', a1: 'QEvent') -> bool:
        if a1.type() == QEvent.HoverEnter:
            self.blink_timer.stop()
        elif a1.type() == QEvent.HoverLeave:
            self.blink_timer.start(1000)
        return super().eventFilter(a0, a1)

    def mousePressEvent(self, event: QtGui.QMouseEvent) -> None:
        logging.info("mousePressEvent")
        if event.button() == Qt.LeftButton:
            self.relative_pos = self.pos() - event.globalPos()
        elif event.button() == Qt.RightButton:
            self.relative_pos = QPoint(0, 0)
        super().mousePressEvent(event)

    def mouseMoveEvent(self, event: QtGui.QMouseEvent) -> None:
        logging.info("mouseMoveEvent")
        if self.relative_pos != QPoint(0, 0):
            logging.info(self.relative_pos)
            self.move(event.globalPos() + self.relative_pos)

        super().mouseMoveEvent(event)

    def mouseReleaseEvent(self, a0: QtGui.QMouseEvent) -> None:
        super().mouseReleaseEvent(a0)
        logging.info("mouseReleaseEvent")
        if a0.button() == Qt.LeftButton:
            self.relative_pos = QPoint(0, 0)


widget_css = """
QWidget#business_card_front{
    background-image: url(:/background/business_card.jpg);
    border: 2px solid qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, 
    stop:0 rgba(225, 215, 0, 255), stop:1 rgba(255, 255, 255, 255));
    border-radius: 1px;
    background-color: transparent;
}

QWidget#business_card_back{
    background-image: url(:/background/business_card.jpg);
    border: 2px solid qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, 
    stop:0 rgba(225, 215, 0, 255), stop:1 rgba(255, 255, 255, 255));
    border-radius: 1px;
    background-color: transparent;
}
"""

if __name__ == "__main__":
    import sys
    import platform
    from PyQt5.QtWidgets import QApplication

    if platform.system() == "Windows":
        import ctypes

        ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID("business card")
    app = QApplication(sys.argv)
    card = BusinessCard()
    sys.exit(app.exec_())
